<?php

namespace app\common\business;

use app\common\model\mysql\SCategory as SCategoryModel;

class SCategory
{
    public $categoryObj = null;
    public function __construct()
    {
        $this->categoryObj = new SCategoryModel();
    }

    /**
     * 查询分类列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalCategory($where,$field){
        $menuData = $this->categoryObj->getNormalCategoryList($where,$field);
        if(!$menuData){
            return [];
        }
        $menuData = $menuData->toArray();
        return $menuData;
    }

    //添加分类
    public function addCategory($data){
        //根据$config_name去数据库查询是否存在这条记录
        $postNameResult = $this->getCategoryByName($data['category_name'],$data['parent_id']);
        if($postNameResult){
            throw new \think\Exception("该分类名称已经存在请重新设置");
        }
        try {
            $this->categoryObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->categoryObj->id;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $postNameResult = $this->getCategoryByName($data['category_name'],$data['parent_id'],$id);
        if($postNameResult){
            throw new \think\Exception("该分类名称已经存在请重新设置");
        }
        $where = [
            "category_id"=>$id,
        ];
        return $this->categoryObj->where($where)->find()->save($data);
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getCategoryById($id){
        $user = $this->categoryObj->getCategoryIdData($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$ids数组，删除数据库记录delete
    public function DelCategoryIds($id){
        //判断此分类下是否有下级
        $where = [
            ["parent_id", "=", $id],
            ["del_flag", "=", 0],
        ];
        $passId = $this->getNormalCategory($where,'category_id');
        if($passId){
            $categoryData = $this->getCategoryById($id);
            throw new \think\Exception($categoryData['category_name']."已有下级,请先删除下级");
        }
        //系统工具已有下级,请先删除下级
        $result = $this->categoryObj->DeleteCategoryIds($id);
        return $result;
    }

    public function getCategoryByName($category_name,$parent_id,$category_id = ''){
        $user = $this->categoryObj->getCategoryByNameModel($category_name,$parent_id,$category_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //分类下拉结构
    public function categoryTree($where,$field){
        $menuData = $this->categoryObj->getCategoryTree($where,$field);
        if(!$menuData){
            return [];
        }
        return $menuData;
    }

    //部门无限极分类
    public function categoryTreeChildren($where = []){
        $deptData = $this->categoryObj->getCategoryTreeChildren(0,$where);
        if(!$deptData){
            return [];
        }
        return $deptData;
    }

    //获取分类多个，用逗号隔开
    public function getCategoryByNames($categoryIds){
        $user = $this->categoryObj->getCategoryByNamesModel($categoryIds);
        if(!$user){
            return [];
        }
        return $user;
    }

    //获取下级分类id(数组)
    public function getCategoryIdsArr($where,$field){
        $categoryIds = $this->categoryObj->getCategoryIdsArrModel($where,$field);
        return $categoryIds;
    }
}
